// 上面这个代码处理过度动画（默认加上不用管）
document.addEventListener('DOMContentLoaded', () => {
    setTimeout(() => {
      document.body.classList.add('sidenav-pinned');
      document.body.classList.add('ready');
    }, 200);
  });
  
  // 每个页面都会导入该js 统一的设置写这里即可
  const toastBox = document.querySelector('#myToast');
  const toast = new bootstrap.Toast(toastBox);
  const tip = (msg) => {
    toastBox.querySelector('.toast-body').innerHTML = msg;
    toast.show();
  };
  
  //设置根地址
  axios.defaults.baseURL = 'http://ajax-api.itheima.net';
  //封装post请求
  const post = (url, data) => {
    return axios({ url, method: 'post', data });
  };
  //封装get请求
  const get = (url, params) => {
    return axios({ url, method: 'get', params });
  };
  //登录用户名的改变
  document.querySelector('#usernameSpan')
    ? (document.querySelector('#usernameSpan').innerHTML =
        localStorage.getItem('username'))
    : '';
  
  //退出
  document.querySelector('#logout')
    ? document.querySelector('#logout').addEventListener('click', function () {
        localStorage.removeItem('username');
        localStorage.removeItem('token');
        location.href = 'login.html';
      })
    : '';
  // 添加请求拦截器
  axios.interceptors.request.use(
    function (config) {
      // 在发送请求之前做些什么
      // console.log(config);
      config.headers.Authorization = localStorage.getItem('token');
      return config;
    },
    function (error) {
      // 对请求错误做些什么
      return Promise.reject(error);
    }
  );
  //添加响应拦截器
  axios.interceptors.response.use(
    function (response) {
      const { data } = response;
      // console.log(data);
      return data;
    },
    function (error) {
      console.dir(error);
      if (error.response.status === 401) {
        location.href = 'login.html';
        localStorage.removeItem('username');
        localStorage.removeItem('token');
      }
      return Promise.reject(error);
    }
  );
  